import {request} from "../../request/index.js"
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wireCode: null,
    user: null,
    wareHouse: null,
    wire: null,
    point: null,
    hiddenmodalput: true,
    returnmodal: true,
    inWareHouse: true,
    array1: [],
    value1: 0,
    wareHouse: '',
  },
  bindChangeWare: function (e) {
    this.setData({
      wareHouse: this.data.array1[e.detail.value]
    })
    this.setData({
      value1: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    var wireCode = options.wireCode;
    that.setData({
      wireCode: wireCode
    })
    console.log(wireCode)
    //获取接地线类，仓库类，管理人类
    request({
      url: "/wire/findWireByCode", 
      data: {
          wireCode: this.data.wireCode
      }, 
      header: {
        'content-type': 'application/json' // 默认值
      }
    })
    .then(res => {
      if(res.statusCode == 500){
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000
        })
        setTimeout(function () {
          wx.switchTab({
            url: '../features/features',
          })
        }, 2000)
      }else{
        that.setData({
          user: res.data.user,
          wareHouse: res.data.wareHouse,
          wire: res.data.wire,
          point: res.data.point
        })
      }
    })
  },
  onUnload: function () {
    wx.redirectTo({
      url: '../myselfWire/myselfWire?notInWareHouseList=' + getApp().globalData.notInWareHouseList
    })
  },
  //点击借出按钮
//   lenWire: function () {
//     request({
//       url: "/wire/lenWire", 
//       data: {
//         wireCode: this.data.wireCode,
//         telephone: wx.getStorageSync('tel')
//       }, 
//       header: {
//         'content-type': 'application/json' // 默认值
//       }
//     })
//     .then(res => {
//       wx.navigateTo({
//         url: './showUsageStatus?wireCode=' + res.data.wireCode,
//       })
//     })
//   },
  //点击开始使用按钮，应跳出提示框提示扫一扫接地点二维码
  beginUse: function () {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },


  //开始使用，添加地址取消/确定
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  confirm: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //点击扫一扫，获取接地点信息
  getPointLocation: function () {
    var that = this;
    wx.scanCode({
      complete: (res) => {
        var result = res.result;
        console.log(result)
        that.setData({
          result: result
        })
        request({
          url: "/wire/scanPointChangeWireUsageStatus", 
          data: {
            wireCode: this.data.wireCode,
            pointId: result
          }, 
          header: {
            'content-type': 'application/json' // 默认值
          }
        })
        .then(res => {
          if (res.statusCode == 200) {
            wx.showToast({
              title: '更新成功',
              icon: 'success',
              duration: 2000
            })
            wx.navigateTo({
            url: './showUsageStatus?wireCode=' + res.data.wireCode,
          })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        })
      },
    })
  },

  inWare: function () {
    request({
      url: "/wire/changeStatus", 
      data: {
        wireCode: this.data.wireCode,
        usageStatus: '使用中'
      }, 
      header: {
        'content-type': 'application/json' // 默认值
      }
    })
    .then(res => {
      wx.navigateTo({
        url: './showUsageStatus?wireCode=' + res.data.wireCode,
      })
    })
  },
  return: function () {
    request({
      url: "/wire/changeStatus", 
      data: {
        wireCode: this.data.wireCode,
        usageStatus: '结束使用'
      }, 
      header: {
        'content-type': 'application/json' // 默认值
      }
    })
    .then(res => {
      wx.navigateTo({
        url: './showUsageStatus?wireCode=' + res.data.wireCode
      })
    })
  },

//   //点击"入库保存"按钮后
//   inWare: function () {
//     this.setData({
//       inWareHouse: !this.data.inWareHouse
//     })
//        var that =this;

//     //查询wareHouse列表
//     request({
//       url: "/wareHouse/queryWareHouseName", 
//       data: {
//       }, 
//       header: {
//         'content-type': 'application/json' // 默认值
//       }
//     })
//     .then(res => {
//       that.setData({
//         array1: res.data,
//         wareHouse: res.data[0]
//       })
//     })
//   },
  inCancel:function(){
    this.setData({
      inWareHouse: true
    });
  },
//   inConfirm:function(){
//     request({
//       url: "/wire/saveWire", 
//       data: {
//         wareHouse:this.data.wareHouse,
//         wireCode:this.data.wireCode
//       }, 
//       header: {
//         'content-type': 'application/json' // 默认值
//       }
//     })
//     .then(res => {
//       wx.navigateTo({
//         url: './showUsageStatus?wireCode='+res.data.wireCode,
//       })
//     })
//   }
})